package org.example.controller;

import org.example.model.AvatarSession;
import org.example.service.AvatarSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/avatar")
public class AvatarController {
    private static final Logger logger = LoggerFactory.getLogger(AvatarController.class);

    @Autowired
    private AvatarSessionManager sessionManager;

    /**
     * 创建数字人会话
     */
    @PostMapping("/session")
    public ResponseEntity<?> createSession() {
        try {
            AvatarSession session = sessionManager.createSession();

            Map<String, Object> response = new HashMap<>();
            response.put("sessionId", session.getSessionId());
            response.put("channel", session.getChannel());

            /*Map<String, Object> response = new HashMap<>();
            response.put("sessionId","dsss");

            StartInstanceResponseBodyDataChannel channel = new StartInstanceResponseBodyDataChannel();
            channel.setAppId("sss");
            channel.setGslb(Arrays.asList("sss"));
            channel.setChannelId("222");

            response.put("channel", channel);*/
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("创建数字人会话失败", e);
            return ResponseEntity.badRequest().body(Map.of(
                    "error", "创建数字人会话失败: " + e.getMessage()));
        }
    }

    /**
     * 发送文本到数字人
     */
    @PostMapping("/session/{sessionId}/text")
    public ResponseEntity<?> sendText(
            @PathVariable String sessionId,
            @RequestParam String text,
            @RequestParam(defaultValue = "false") boolean interrupt) {
        try {
            sessionManager.sendText(sessionId, text, interrupt);
            return ResponseEntity.ok(Map.of("success", true));
        } catch (Exception e) {
            logger.error("向数字人发送文本失败", e);
            return ResponseEntity.badRequest().body(Map.of(
                    "error", "向数字人发送文本失败: " + e.getMessage()));
        }
    }

    /**
     * 打断数字人当前播报
     */
    @PostMapping("/session/{sessionId}/interrupt")
    public ResponseEntity<?> interruptSpeaking(@PathVariable String sessionId) {
        try {
            sessionManager.interruptSpeaking(sessionId);
            return ResponseEntity.ok(Map.of("success", true));
        } catch (Exception e) {
            logger.error("打断数字人播报失败", e);
            return ResponseEntity.badRequest().body(Map.of(
                    "error", "打断数字人播报失败: " + e.getMessage()));
        }
    }

    /**
     * 关闭数字人会话
     */
    @PostMapping("/session/{sessionId}/close")
    public ResponseEntity<?> closeSession(@PathVariable String sessionId) {
        try {
            sessionManager.closeSession(sessionId);
            return ResponseEntity.ok(Map.of("success", true));
        } catch (Exception e) {
            logger.error("关闭数字人会话失败", e);
            return ResponseEntity.badRequest().body(Map.of(
                    "error", "关闭数字人会话失败: " + e.getMessage()));
        }
    }

    /**
     * 获取数字人会话状态
     */
    @GetMapping("/session/{sessionId}")
    public ResponseEntity<?> getSessionStatus(@PathVariable String sessionId) {
        AvatarSession session = sessionManager.getSession(sessionId);
        if (session == null) {
            return ResponseEntity.notFound().build();
        }

        Map<String, Object> response = new HashMap<>();
        response.put("sessionId", session.getSessionId());
        response.put("status", session.getStatus());
        response.put("duration", session.getDuration());

        return ResponseEntity.ok(response);
    }
}